/*
 * File     : NativeStringImpl.java
 * Created  : 26 May 2011
 *
 * Copyright © 2011 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dni.internal.string;

import com.googlecode.dni.internal.DniInternal;
import com.googlecode.dni.type.NativeObject;
import com.googlecode.dni.type.NativeString;
import com.googlecode.dni.type.Pointer;

/**
 * Basic implementation of {@link NativeString}.
 *
 * @author Matthew Wilson
 */
public final class NativeStringImpl implements NativeString
{

    private final Pointer pointer;

    private final int maximumLength;


    /**
     * @param pointer
     *            the pointer to the string
     * @param maximumLength
     *            the maximum length of the string
     */
    public NativeStringImpl( final Pointer pointer,
                             final int maximumLength )
    {
        assert pointer != null;
        assert pointer.address() != 0L;
        assert maximumLength > 0;
        this.pointer = pointer;
        this.maximumLength = maximumLength;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Pointer pointer()
    {
        return this.pointer;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isSameObject( final NativeObject other )
    {
        return other instanceof NativeString && other.pointer().equals( this.pointer );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void free()
    {
        DniInternal.ALLOCATOR.free( this.pointer );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String read( final String charset )
    {
        return FastCharset.getInstance( charset ).decode( this.pointer.address() );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals( final Object obj )
    {
        if ( !( obj instanceof NativeStringImpl ) )
        {
            return false;
        }

        NativeStringImpl other = (NativeStringImpl) obj;
        return other.pointer().address() == this.pointer.address();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode()
    {
        return this.pointer.hashCode();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString()
    {
        return "NativeString[" + this.pointer + " + 0x" + Integer.toHexString( this.maximumLength ) + "]";
    }

}
